package com.suke.czx.modules.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.suke.czx.common.utils.FieldConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.project.entity.Project;
import com.suke.czx.modules.project.entity.ProjectSearchBo;
import com.suke.czx.modules.project.mapper.ProjectMapper;
import com.suke.czx.modules.project.service.ProjectService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 项目
 * 代码描述: 项目业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/07 10:15:11
 */
@Service
@AllArgsConstructor
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    private final ProjectMapper projectMapper;

    /**
     * 分页查询项目列表
     * @param projectSearchBo 查询BO
     * @return VO
     */
    @Override
    public R pageProjectList(ProjectSearchBo projectSearchBo) {
        Page<Project> page = PageHelper.startPage(projectSearchBo.getPage(), projectSearchBo.getLimit(), true,true,true);
        //获取项目查询结果集
        List<Project> projectList = projectMapper.pageProjectList(projectSearchBo);
        return Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, page.getTotal())).setData(Optional.ofNullable(projectList).orElse(Lists.newArrayList()));
    }

    /**
     * 更新项目状态
     * @param projectId 项目编码
     * @param status 项目状态
     * @param userId 操作者编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateProjectStatus(Long projectId, Integer status, Long userId) {
        projectMapper.updateDelProject(projectId, status, userId);
        return R.ok();
    }

}
